﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WaterBill.BUS;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraReports.UI;

namespace WaterBill.GUI
{
    public partial class frmManageBill : DevExpress.XtraEditors.XtraForm
    {
        BUS_Customers busCus = new BUS_Customers();
        BUS_Bills busBill = new BUS_Bills();
        BUS_Charges busCharge = new BUS_Charges();
        public frmManageBill()
        {
            InitializeComponent();
        }

        private void frmManageOrder_Load(object sender, EventArgs e)
        {
            loadDataForm();
        }

        private void loadDataForm()
        {
            try
            {
                gcCustomer.DataSource = busCus.BUS_Customers_GetStatusEnable();
                gcBill.DataSource = null;
                DataTable dtCharge = busCharge.BUS_Charges_GetAll();
                DataRow dr = dtCharge.NewRow();
                dr["NameCharge"] = "--Chọn--";
                dr["ChargeId"] = 0;
                dtCharge.Rows.InsertAt(dr, 0);

                lkCharge.Properties.DataSource = dtCharge;
                lkCharge.Properties.DisplayMember = "NameCharge";
                lkCharge.Properties.ValueMember = "ChargeId";
                lkCharge.ItemIndex = 0;
            }
            catch
            {
                XtraMessageBox.Show("Lỗi, đã xảy ra lỗi không xác định. Xin vui lòng thử lại.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }


        private void gvCustomer_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                int CustomerId = Convert.ToInt32(((GridView)sender).GetRowCellValue(((GridView)sender).FocusedRowHandle, "CustomerId"));
                gcBill.DataSource = busBill.BUS_Bills_GetByCustomerId(CustomerId);
                this.chkSelectAll.Checked = false;
                this.lkCharge.ItemIndex = 0;
                gvBill.Columns[2].Visible = false;
                gvBill.Columns[3].Visible = false;
                gvBill.Columns[4].Visible = false;
                gvBill.Columns[1].GroupIndex = 1;
                gvBill.ExpandAllGroups();              
            }
            catch
            {
                XtraMessageBox.Show("Lỗi, đã xảy ra lỗi không xác định. Xin vui lòng thử lại.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }


        private void gvCustomer_RowClick(object sender, RowClickEventArgs e)
        {
            gvCustomer_FocusedRowChanged(sender, null);
        }

        private void chkSelectAll_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.chkSelectAll.Checked == true)
                {
                    gvCustomer.SelectAll();
                    if (Convert.ToInt32(this.lkCharge.EditValue.ToString()) == 0)
                    {
                        gcBill.DataSource = busBill.BUS_Bills_GetAll();
                        gvBill.Columns[1].VisibleIndex = 0;
                        gvBill.Columns[1].GroupIndex = 1;
                        gvBill.ExpandAllGroups();
                    }
                }
                else
                {
                    gvBill.Columns[1].UnGroup();
                    for (int i = 0; i < gvCustomer.RowCount; i++)
                    {
                        gvCustomer.UnselectRow(i);
                    }
                    gcBill.DataSource = null;
                }
            }
            catch
            {
                XtraMessageBox.Show("Lỗi, đã xảy ra lỗi không xác định. Xin vui lòng thử lại.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
        }

        private void chkSelectAll_Click(object sender, EventArgs e)
        {
            lkCharge.ItemIndex = 0;
        }

        private void lkCharge_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                int ChargeId = Convert.ToInt32(this.lkCharge.EditValue.ToString());
                if (ChargeId != 0)
                {
                    this.chkSelectAll.Checked = true;
                    gvBill.Columns[1].UnGroup();
                    gvBill.Columns[2].Visible = true;
                    gvBill.Columns[3].Visible = true;
                    gvBill.Columns[4].Visible = true;
                    gvBill.Columns[1].VisibleIndex = 0;
                    gvBill.Columns[2].VisibleIndex = 1;
                    gvBill.Columns[3].VisibleIndex = 2;
                    gvBill.Columns[4].VisibleIndex = 3;
                    gcBill.DataSource = busBill.BUS_Bills_GetByChargeId(ChargeId);
                }
            }
            catch
            {
                XtraMessageBox.Show("Lỗi, đã xảy ra lỗi không xác định. Xin vui lòng thử lại.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            splashScreenManager1.ShowWaitForm();
            loadDataForm();
            splashScreenManager1.CloseWaitForm();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            this.gvCustomer.MoveNext();
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            this.gvCustomer.MovePrev();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet dsBill = new DataSet();
                DataTable dtBill = new DataTable();
                dtBill.Columns.Add("FullName", typeof(string));
                dtBill.Columns.Add("Address", typeof(string));
                dtBill.Columns.Add("Phone", typeof(string));
                dtBill.Columns.Add("RegisteredDate", typeof(DateTime));
                dtBill.Columns.Add("BillNumber", typeof(string));
                dtBill.Columns.Add("NameCharge", typeof(string));
                dtBill.Columns.Add("OldIndex", typeof(int));
                dtBill.Columns.Add("NewIndex", typeof(int));
                dtBill.Columns.Add("UsedIndex", typeof(int));
                dtBill.Columns.Add("TotalPrice", typeof(decimal));
                dtBill.Columns.Add("Dathu", typeof(string));
                dtBill.Columns.Add("Price", typeof(decimal));
                DataRow dr;
                for (int i = 0; i < gvBill.RowCount; i++)
                {
                    dr = dtBill.NewRow();
                    dr = (DataRow)gvBill.GetDataRow(i);
                    dtBill.ImportRow(dr);
                }
                dsBill.Tables.Add(dtBill);
                if (int.Parse(this.lkCharge.EditValue.ToString()) == 0)
                {
                    rptListBillByCustomer rptBill = new rptListBillByCustomer();
                    rptBill.DataSource = dsBill;
                    rptBill.DataMember = dsBill.Tables[0].TableName;
                    GroupField groupfield = new GroupField();
                    groupfield.FieldName = "FullName";
                    rptBill.GroupHeader1.GroupFields.Add(groupfield);
                    rptBill.lblFullName.DataBindings.Add("Text", null, "FullName");
                    rptBill.lblAddress.DataBindings.Add("Text", null, "Address");
                    rptBill.lblPhone.DataBindings.Add("Text", null, "Phone");
                    rptBill.lblRegisteredDate.DataBindings.Add("Text", null, "RegisteredDate", "{0:dd/MM/yyyy}");
                    rptBill.lblBillNumber.DataBindings.Add("Text", null, "BillNumber");
                    rptBill.lblNameCharge.DataBindings.Add("Text", null, "NameCharge");
                    rptBill.lblOldIndex.DataBindings.Add("Text", null, "OldIndex");
                    rptBill.lblNewIndex.DataBindings.Add("Text", null, "NewIndex");
                    rptBill.lblUsedIndex.DataBindings.Add("Text", null, "UsedIndex");
                    rptBill.lblPrice.DataBindings.Add("Text", null, "Price", "{0:0,0 vnđ}");
                    rptBill.lblTotalPrice.DataBindings.Add("Text", null, "TotalPrice", "{0:0,0 vnđ}");
                    rptBill.lblStatus.DataBindings.Add("Text", null, "Dathu");
                    rptBill.PrintDialog();
                }
                else
                {
                    rptListBillByCharge rptBill = new rptListBillByCharge();
                    rptBill.DataSource = dsBill;
                    rptBill.DataMember = dsBill.Tables[0].TableName;
                    rptBill.lblFullName.DataBindings.Add("Text", null, "FullName");
                    rptBill.lblAddress.DataBindings.Add("Text", null, "Address");
                    rptBill.lblBillNumber.DataBindings.Add("Text", null, "BillNumber");
                    rptBill.lblNameCharge.DataBindings.Add("Text", null, "NameCharge");
                    rptBill.lblPrice.DataBindings.Add("Text", null, "Price", "{0:0,0 vnđ}");
                    rptBill.lblOldIndex.DataBindings.Add("Text", null, "OldIndex");
                    rptBill.lblNewIndex.DataBindings.Add("Text", null, "NewIndex");
                    rptBill.lblUsedIndex.DataBindings.Add("Text", null, "UsedIndex");
                    rptBill.lblTotalPrice.DataBindings.Add("Text", null, "TotalPrice", "{0:0,0 vnđ}");
                    rptBill.lblStatus.DataBindings.Add("Text", null, "Dathu");
                    rptBill.PrintDialog();
                }
            }
            catch
            {
                XtraMessageBox.Show("Lỗi, đã xảy ra lỗi không xác định. Xin vui lòng thử lại.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }   

        }

        
        
    }
}